<?php

namespace App\Jobs;

use App\Http\service\GzhService;
use App\Models\Gzh as GzhModel;
use App\Models\User as UserModel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use function app\Utils\go;

class GzhSendMessageJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data=$data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $data=$this->data;
        $user=UserModel::where("id",$data["id"])->first();
        if(!$user){
            return;
        }

        if (!$user["unionid"]){
            return;
        }
        $gzh_openid=GzhModel::where("unionid",$user["unionid"])->first();
        if(!$gzh_openid){
            return;
        }

        $send=[
            "openid"=>$gzh_openid["openid"],
            "name"=>$user["nickname"],
            "message"=>$data["message"]!=''?$data["message"]:"[图片]",
        ];

        (new GzhService())->send_message($send);
    }
}
